package com.we.mexico.flink.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class WeMexicoCpFile {
    public static Logger LOG = LoggerFactory.getLogger(WeMexicoCpFile.class);

    public static void copyFile(String srcFile, String dstFile) {
        try {
            InputStream inputStream =
                    WeMexicoCpFile.class.getClassLoader().getResourceAsStream(srcFile);
            File file = new File(dstFile);
            System.out.println(file.getPath());
            if (file.exists()) {
                file.delete();
            } else {
                file.createNewFile();
            }
            //            if (!file.getParentFile().exists()) {
            //                LOG.info("创建上层文件夹");
            //                file.getParentFile().mkdir();
            //            } else {
            //                LOG.info("先删除再创建");
            //                file.getParentFile().delete();
            //                file.getParentFile().mkdir();
            //            }

            BufferedInputStream in = new BufferedInputStream(inputStream);
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile));

            int len = -1;
            byte[] b = new byte[1024];
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }

            in.close();
            out.close();

        } catch (Exception e) {
            LOG.error("WeMexicoCpFile excep: " + e);
        }
    }

    public static String getHiveSiteFile() {
        String hiveConfDir = "/tmp/test";
        String dstFile = "/tmp/test/hive-site.xml";
        File file = new File(dstFile);
        if (!file.exists()) {
            System.out.println("##cp file!!");
            WeMexicoCpFile.copyFile("hiveconf/hive-site.xml", dstFile);
            System.out.println("##cp file done!!");
        }
        return hiveConfDir;
    }

    public static void main(String[] args) {
        copyFile("hiveconf/hive-site.xml", "/tmp/hive-site1.xml");
        System.out.println("done!!");
    }
}
